import {
  HTTP
} from '../utils/http.js';

class UserModel extends HTTP {

  /**
   * 用户行为操作
   * @param {Long} contentId 
   * @param {Long} userId
   * @param {String} cacheKey 
   * @param {String} opsType 
   */
  ops(contentId, cacheKey, opsType = 'ops', userId = "") {
    return this.request({
      url: `user/${opsType === 'ops' ? 'ops' : 'operated'}`,
      data: {
        "token": wx.getStorageSync('token'),
        "contentId": contentId,
        "cacheKey": cacheKey,
        "userId": userId
      },
      method: "POST"
    });
  }

  /**
   * 获取用户信息
   */
  getUserInfo() {
    return this.request({
      url: 'userIndex/findUserInfo',
      data: {
        "token": wx.getStorageSync('token'),
      },
      method: "POST"
    });
  }

  /**
   * 根据ID查询
   * @param {Long} userId 
   */
  getUserInfoById(userId) {
    return this.request({
      url: `userIndex/${userId}`,
    });
  }

  /**
   * 关注
   * @param {Long} followId 
   */
  follow(followId, type) {
    return this.request({
      url: 'user/follow',
      data: {
        "token": wx.getStorageSync('token'),
        "followId": followId,
        "type": type
      },
      method: "POST"
    });
  }

  /**
   * 是否关注
   * @param {Long} followId 
   */
  followed(followId) {
    return this.request({
      url: 'user/followed',
      data: {
        "token": wx.getStorageSync('token'),
        "followId": followId,
      },
      method: "POST"
    });
  }

  /**
   * 获取我的关注
   * @param {Integer} pageNum 
   * @param {Integer} pageSize 
   */
  followers(pageNum, pageSize = 50) {
    return this.request({
      url: 'userIndex/followers',
      data: {
        "token": wx.getStorageSync('token'),
        "pageNum": pageNum,
        "pageSize": pageSize
      },
      method: "POST"
    });
  }

  /**
   * 获取我的粉丝
   * @param {Integer} pageNum 
   * @param {Integer} pageSize 
   */
  fans(pageNum, pageSize = 50) {
    return this.request({
      url: 'userIndex/fans',
      data: {
        "token": wx.getStorageSync('token'),
        "pageNum": pageNum,
        "pageSize": pageSize
      },
      method: "POST"
    });
  }

  /**
   * 获取用户的关注
   * @param {Long} userId 
   * @param {Integer} pageNum 
   * @param {Integer} pageSize 
   */
  userFollowers(userId, pageNum, pageSize = 50) {
    return this.request({
      url: 'userIndex/user/followers',
      data: {
        "token": wx.getStorageSync('token'),
        "userId": userId,
        "pageNum": pageNum,
        "pageSize": pageSize
      },
      method: "POST"
    });
  }

  /**
   * 获取用户的粉丝
   * @param {Long} userId 
   * @param {Integer} pageNum 
   * @param {Integer} pageSize 
   */
  userFans(userId, pageNum, pageSize = 50) {
    return this.request({
      url: 'userIndex/user/fans',
      data: {
        "token": wx.getStorageSync('token'),
        "userId": userId,
        "pageNum": pageNum,
        "pageSize": pageSize
      },
      method: "POST"
    });
  }

  /**
   * 校验是否是自己
   * @param {Long} userId 
   */
  self(userId) {
    return this.request({
      url: 'user/self',
      data: {
        "token": wx.getStorageSync('token'),
        "userId": userId,
      },
      method: "POST"
    });
  }

  /**
   * 搜索
   * @param {String} q 
   * @param {Long} pageNum 
   * @param {Long} pageSize 
   */
  search(q, pageNum, pageSize = 50) {
    return this.request({
      url: "userIndex/search",
      data: {
        "token": wx.getStorageSync('token'),
        "nickName": q,
        "pageNum": pageNum,
        "pageSize": pageSize
      },
      method: 'POST'
    })
  }

  /**
   * 删除历史记录
   * @param {Long} contentId 
   */
  delViewHistory(contentId) {
    return this.request({
      url: 'user/delViewHistory',
      data: {
        "token": wx.getStorageSync('token'),
        "contentId": contentId,
      },
      method: "POST"
    });
  }

  /**
   * 取消我的收藏
   * @param {Long} contentId 
   */
  cancelMyCollect(contentId) {
    return this.request({
      url: 'user/cancelMyCollect',
      data: {
        "token": wx.getStorageSync('token'),
        "contentId": contentId,
      },
      method: "POST"
    });
  }

  /**
   * 校验用户昵称重复
   * @param {String} nickName 
   */
  check(nickName) {
    return this.request({
      url: 'user/check',
      data: {
        "nickName": nickName,
      },
      method: "POST"
    });
  }

  /**
   * 修改
   * @param {Object} user 
   */
  modify(user) {
    return this.request({
      url: 'user/modify',
      data: user,
      method: "POST"
    });
  }

  /**
   * 用户文件修改
   * @param {String} baseUrl 
   * @param {String} filePath 
   * @param {Object} user 
   * @param {String} type 
   */
  modifyWithFile(baseUrl, filePath, user, type = 'avatar') {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: `${baseUrl}user/userFile`,
        filePath: filePath,
        name: 'file',
        header: {
          'content-type': 'multipart/form-data'
        },
        formData: {
          "type": type,
          "user": JSON.stringify(user)
        },
        success: (res) => {
          const code = res.statusCode.toString();
          if (code.startsWith('2')) {
            resolve(JSON.parse(res.data));
          } else {
            reject();
          }
        },
        fail: (err) => {
          reject();
        }
      })
    })
  }

}

export {
  UserModel
}